const fs = require('fs');
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const tee = require("@babel/types");
const generator = require("@babel/generator").default;
const template = require("@babel/template").default;

let yuanwj = fs.readFileSync('./al.js', {encoding: "utf-8"});
let ast = parser.parse(yuanwj);


let jiexswitch = {

}

let shuz = {};
let gxcc = {};
let hansyx = [];

function getlast(obj) {
    // try {
    if (obj[obj.length - 2].type === "ReturnStatement") {
        return "retu"
    }
    let lt = obj[obj.length - 2].expression.right;
    let gu;
    if (lt.type === "ConditionalExpression") {
        gu = [lt.consequent.value, lt.alternate.value, obj[obj.length - 2].expression.left];
    }else {
        gu = [lt.value]
    }
    return gu
    // }catch(e){
    //     debugger
    // }
}

function start(index, isfor, ispand){

    let cz = hansyx.indexOf(index);
    if (cz != -1 && cz < ispand ) {
        gxcc
        return {"type": "goto", wher:cz}
    }else if (cz != -1 && cz > ispand ) {

        return 
    }
    hansyx.push(index);
    let getwz = shuz[index];
    let dt = getlast(getwz);
    if (dt === 'retu'){
        gxcc[index] = {"type": "return"}
        return
    }

    if (dt.length != 1){
        let g = dt[0];
        let g2 = dt[1];
        let st = hansyx.length;
        if (st == 1079){
            debugger
        }
        let k = start(g, isfor, st)
        if (k){
            gxcc[g] = k
        }
        let st2 = hansyx.length;
        k = start(g2, isfor, hansyx.length)
        if (k){
            gxcc[index] = k
        }
        if (!gxcc[index]){
            gxcc[index] = {}
        }
        gxcc[index].cbb = {"type": "ifelse", start: st, stel: st2, end: hansyx.length}
    }else{
        let k = start(dt[0], isfor, ispand)
        if (k){
            gxcc[index] = k
        }
    }
}

let parserData = {
    CallExpression(path){
        if (path.node.callee.name === "Cbb"){
            
            let sei = path.parentPath.container[1];
            for (let i = 0; i < sei.cases.length; i++){
                shuz[sei.cases[i].test.value] = sei.cases[i].consequent;

            }
            start(13058, 0,0)
        }
    }

}



traverse(ast,parserData)
console.log()


